﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.DAL;

namespace WCFMINERP.BLL.PhotoBll
{
   public  class PhotoinfoBll
    {
       /// <summary>
       /// 根据店铺ID获取数据
       /// </summary>
       /// <param name="outshopid"></param>
       /// <returns></returns>
       public List<CusFileInfo> GetFileInfo(int outshopid)
       {
           List<CusFileInfo> listinfo = new List<CusFileInfo>();
           using (newSampleYPDBEntities db = new newSampleYPDBEntities())
           {
               List<T_PhotoDownload> modelList = db.T_PhotoDownload.Where(p => p.OutShopId == outshopid && p.State==0).ToList();  //获取未下载的
               foreach (T_PhotoDownload down in modelList)
               {
                   CusFileInfo fileinfo = new CusFileInfo();
                   fileinfo.FileLength = 0;
                   fileinfo.FileName = System.IO.Path.GetFileName(down.PicPath);
                   fileinfo.MapPath = down.Id + "/" + fileinfo.FileName;
                   fileinfo.filepath = down.PicPath;
                   fileinfo.Photoid = down.Photoid;                  
                   listinfo.Add(fileinfo);
               }
           }
           return listinfo;
       }

       /// <summary>
       /// 更改下载图片的状态
       /// </summary>
       /// <returns></returns>
       public bool UpdatePhotoState(int shopid,int photoid,int state)
       {
           using (newSampleYPDBEntities db = new newSampleYPDBEntities())
           {
             T_PhotoDownload model=   db.T_PhotoDownload.Where(p => p.Photoid == photoid && p.OutShopId == shopid).FirstOrDefault();
             model.State = state;

             return db.SaveChanges() > 0;
           }
       }

       public string GetServerPhoto()
       {
           using (newSampleYPDBEntities db = new newSampleYPDBEntities())
           {
               FileDirPath model = db.FileDirPath.Where(p => p.Des == "已开单").FirstOrDefault();
               return model.FileDir;
           }
       }


       public CusFileInfo GetServerPhotoModel(int DownID)
       {
           using (newSampleYPDBEntities db = new newSampleYPDBEntities())
           {
               T_PhotoDownload model = db.T_PhotoDownload.Where(p => p.Id == DownID).FirstOrDefault();

               if (model == null)
               {
                   return null;
               }
               CusFileInfo fileinfo = new CusFileInfo();
               fileinfo.outshopid = model.OutShopId;
               FileInfo fileInfo = new FileInfo(model.PicPath);
               fileinfo.FileLength = fileInfo.Length;
               fileinfo.FileName = System.IO.Path.GetFileName(model.PicPath);
               fileinfo.MapPath = model.Id + "/" + fileinfo.FileName;
               fileinfo.filepath = model.PicPath;
               fileinfo.Photoid = model.Photoid;

               return fileinfo;
           }
       }
    }   
}
